package edu.imac.angryfood.states;


import java.util.ArrayList;


import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.gui.MouseOverArea;
import org.newdawn.slick.gui.TextField;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;
import edu.imac.angryfood.game.Angryfood;
import edu.imac.angryfood.utils.Account;
import edu.imac.angryfood.utils.PlayersHost;

public class SelectPlayerState extends BasicGameState implements ComponentListener{

	public static int ID;
	public Angryfood parent;
	private int spacing = 50;
	/** The areas defined */
	private MouseOverArea[] areas;
	private MouseOverArea[] areasSup;
	private MouseOverArea[] areasConfirm;
	/** The logo of the game */
	private Image logo;
	
	/** Pictures in order to confirm */
	private Image confirm;
	private Image confirmYes;
	private Image confirmNo;
	/**Pseudo */
	private Image pseudo;
	/** Best score*/
	private Image best_score;
	private Image sup;
	/** Next player */
	private Image nouveau_joueur;
	/** The background image */
	private Image background;
	/**Area to enter a new player */
	private TextField NewPlayer;
	private Font font;
	/**A new player */
	private String NewName = "";
	private Image cadre; 
	/**List of all the players who are stocked in the xml file */
	private PlayersHost PlayersList = PlayersHost.getInstance();
	/**List of all of the accounts the players */
	private ArrayList<Account> ListComptes;
	/**Flag set to indicate to create a new panel of delete one */
	private int flag_new_account;
	private int flag_sup;
	private int flag_panel;

	private StateBasedGame game;
	
	public SelectPlayerState(Angryfood game, int ID) {
		super();
		SelectPlayerState.ID = ID;
		this.parent = game;
	}
	
	/** initializes the game
	 * @param container contains the game
	 * @param game the game
	 * @throws SlickException
	 */
	
	public void init(GameContainer container, StateBasedGame game) throws SlickException {

		
		this.game = game;
		
		/** Initialization of the flags */
		flag_new_account = 0;
		flag_sup = 0;
		flag_panel = -1;
		
		/** Initialization of the pictures */
		logo =  new Image("res/img/menu/logoAF.png");
		confirm =  new Image("res/img/menu/confirm.png");
		confirmYes =  new Image("res/img/menu/confirm1.jpg");
		confirmNo =  new Image("res/img/menu/confirm2.jpg");
		background = new Image("res/img/menu/fondAF.jpg");
		pseudo =  new Image("res/img/menu/pseudo.png");
		best_score = new Image("res/img/menu/best_score.png");
		nouveau_joueur = new Image("res/img/menu/nouveau_joueur.png");
		cadre = new Image("res/img/menu/cadre.png");
		sup = new Image("res/img/menu/supPlayer.jpg");
		font = new AngelCodeFont("res/font/demo2.fnt","res/font/demo2_00.tga");
		
		
		/** Loading of the players list from the XML */
		PlayersList.load();
		ListComptes = PlayersList.getComptes();

		/** Textfield to enter a new player */
		NewPlayer = new TextField(container, font, 50,525,500,35, new ComponentListener() {
			public void componentActivated(AbstractComponent source) {
				NewName = "";
				/** Check of the validity of the pseudo */
				if(PlayersList.isPseudo(NewPlayer.getText()) && ListComptes.size() <= 6){
						Account NewCompte = new Account(NewPlayer.getText(), NewPlayer.getText(), "1_1", 0);
				/** Check if the players doesn't exist already*/
						if(PlayersList.verifCompte(NewCompte)){
							PlayersList.addCompte(NewCompte);
							flag_new_account =1;
							NewPlayer.setText("");
						}
				}
			}
		});
		 
	/** There is no additional panel, initialization of the mouse areas */
	if(flag_new_account ==0 || flag_sup ==0){
		areas = new MouseOverArea[ListComptes.size()];
		areasSup = new MouseOverArea[ListComptes.size()];
		for (int cpt=0;cpt<ListComptes.size();cpt++) {	
			areas[cpt] = new MouseOverArea(container, cadre, 50, 150 + cpt *spacing, 200, 50, this);
			areas[cpt].setNormalColor(new Color(1,1,1,0.8f));
			areas[cpt].setMouseOverColor(new Color(1,1,1,1f));
			areasSup[cpt] = new MouseOverArea(container, sup, 500, 160 + cpt *spacing, 25, 25, this);
		}
		
	}
	
	/** Initialization of the mouse areas of the confirmation panel */
	areasConfirm = new MouseOverArea[2];
	areasConfirm[0] = new MouseOverArea(container, confirmYes, 250, 300, 100, 50, this);
	areasConfirm[1] = new MouseOverArea(container, confirmNo, 400, 300, 100, 50, this);
}

	/** render the graphics in slick
	 * @param container contains the game
	 * @param game the game
	 * @param g the graphics
	 */

	public void render(GameContainer container, StateBasedGame game, Graphics g) {
		
		/** Rendering of the pictures*/
		background.draw(0, 0, 800, 600);
		logo.draw(400,0,400,100);
		pseudo.draw(50, 100, 200, 50);
		best_score.draw(250,100,200,50);
		nouveau_joueur.draw(50,475,200,50);


		/** Rendering of the areas and redrawing, the list of the players has been modified (a flag is up) */
		if(flag_new_account ==1 || flag_sup == 1){
			areas = new MouseOverArea[ListComptes.size()];
			areasSup = new MouseOverArea[ListComptes.size()];
			for (int cpt=0;cpt<ListComptes.size();cpt++) {	
				areas[cpt] = new MouseOverArea(container, cadre, 50, 150 + cpt *spacing, 200, 50, this);
				areas[cpt].setNormalColor(new Color(1,1,1,0.8f));
				areas[cpt].setMouseOverColor(new Color(1,1,1,1f));
				
				areasSup[cpt] = new MouseOverArea(container, sup, 500, 160 + cpt *spacing, 25, 25, this);
			}

			flag_new_account =0;
			flag_sup = 0;
		}
				
		
		
		/** The list of the players is displayed */
		for (int i=0;i<ListComptes.size();i++) {
			areas[i].render(container, g);
			areasSup[i].render(container, g);
			g.drawString(this.ListComptes.get(i).getName(), 100 , 160 + i *spacing);
			cadre.draw(250,150+ i*spacing,200,50);
			g.drawString(String.valueOf(this.ListComptes.get(i).getMax_score()), 300 , 160 + i *spacing);
		}
		
		/** Rendering of the confirmation panel */
		if(flag_panel !=-1){
			confirm.draw(275,200,200,50);
			areasConfirm[0].render(container, g);
			areasConfirm[1].render(container, g);	
		}
		NewPlayer.render(container, g);

		
		g.setFont(font);
		g.drawString(NewName, 200, 550);

		
	}

	/** update the physic world
	 * @param container the game container
	 * @param game the game
	 * @param delta
	 */	
	public void update(GameContainer container, StateBasedGame game, int delta) {
	}

	/**
     * Function used to click on the areas
     * 
     * @param source Area of the screen which is clicked
     */
	
	@Override
	public void componentActivated(AbstractComponent source) {	
		
		/** Clic on the player and set him at the current player */
		for (int i=0;i<ListComptes.size();i++) {
			if (source == areas[i]) {
				parent.load_player = false;
				parent.setCurrentPlayer(ListComptes.get(i));
				game.enterState(3, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
				
			}
			
		/** Panel of suppression */
			if (source == areasSup[i]) {
				flag_panel =i;
			}	
		}
		
		/** Suppression of a player */
		if (source == areasConfirm[0]) {
			 PlayersList.deleteComptes(this.ListComptes.get(flag_panel));	
		     flag_sup = 1;
			 flag_panel =-1;
		}
				
		if (source == areasConfirm[1]) {
			 flag_panel = -1;
		}
	}
		
	
		
	

	@Override
	public int getID() {
		return SelectPlayerState.ID;
	}


}
